package eg.backend.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eg.backend.base.repository.EntityRepositoryImpl;
import eg.backend.domain.GlobalShop;
import eg.backend.domain.Shop;
import eg.backend.repository.ShopRepository;
import eg.backend.util.StringUtil;

public class ShopRepositoryImpl extends EntityRepositoryImpl<Shop, String> implements ShopRepository {
	@Override
	public Shop get(String identifier) {
		return this.baseDao.get(identifier, Shop.class);
	}
	
	public List<Shop> findUsedShops(String owner) {
		String queryName = "Shop.findUsedShops";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		return this.findByNamedQuery(queryName, queryParams);
	}

	public GlobalShop getGlobalShop(String name) {
		String queryName = "Shop.getGlobalShop";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("name", name);
		return baseDao.getByNamedQuery(queryName, queryParams);
	}

	public List<String> findByNameAndOwner(String name, String owner) {
		String queryName = "Shop.findByOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		if(!StringUtil.isEmpty(name)) {
			queryName = "Shop.findByNameAndOwner";
			queryParams.put("name", name);
		}
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

	public List<String> findNotUsedByNameAndOwner(String name, String owner) {
		String queryName = "Shop.findNotUsedByOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		if(!StringUtil.isEmpty(name)) {
			queryName = "Shop.findNotUsedByNameAndOwner";
			queryParams.put("name", name);
		}
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

	public Shop getByNameAndOwner(String globalShop, String owner) {
		String queryName = "Shop.getByNameAndOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		queryParams.put("globalShop", globalShop);
		return this.getByNamedQuery(queryName, queryParams);
	}

	public List<String> findAllGlobalShopNames() {
		String queryName = "Shop.findAllGlobalShopNames";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

}
